package cn.controller;

import cn.entity.Result;
import cn.entity.SysArea;
import cn.service.SysAreaSerevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/area")
public class SysAreaController {
    @Autowired
    SysAreaSerevice sysAreaSerevice;

    @RequestMapping("")
    public ModelAndView area(){
        return new ModelAndView("area/area");
    }
    @RequestMapping("index/{pageNum}/{pageSize}")
    public Result selectPage(@PathVariable("pageNum")int pageNum, @PathVariable("pageSize")int pageSize, @RequestBody HashMap<String,Object> map){
        return new Result(true,"成功",sysAreaSerevice.selectPage(pageNum,pageSize,map));
    }

    @RequestMapping("selectTree")
    public Result selectTree(){
        SysArea sysArea = new SysArea();
        sysArea.setDelFlag("0");
        return new Result(true,"成功",sysAreaSerevice.select(sysArea));
    }

    @RequestMapping("selectChangeTree/{id}")
    public Result selectChangeTree(@PathVariable("id")Long id){
        return new Result(true,"成功",sysAreaSerevice.selectChangeTree(id));
    }

    @RequestMapping("/deleteOne/{id}")
    public int deleteOne(@PathVariable Long id){
        SysArea sysArea = sysAreaSerevice.selectByPrimaryKey(id);
        String s = sysAreaSerevice.selectParentIds(String.valueOf(id));
        SysArea changeParentIds = new SysArea();
        changeParentIds.setNewId(s);
        changeParentIds.setParentIds(s+id+",");
        changeParentIds.setId(id);
        changeParentIds.setParentId(id);
        changeParentIds.setNewParentId(String.valueOf(sysArea.getParentId()));
        sysAreaSerevice.updateParentIds(changeParentIds);
        sysArea.setDelFlag("1");
        sysArea.setUpdateDate(new Date());
        return sysAreaSerevice.updateByPrimaryKeySelective(sysArea);
    }


    @RequestMapping("/showUpdate")
    public SysArea showUpdate(@RequestBody HashMap<String,Object> map){
        return sysAreaSerevice.selectArea(map);
    }

    @RequestMapping("doUpdate")
    public int doUpdate(@RequestBody SysArea sysArea){
        sysArea.setUpdateDate(new Date());
        String newId = sysArea.getNewId();
        if (newId!=null){
            String s = sysAreaSerevice.selectParentIds(newId);
            s=s+newId+",";
            SysArea areaParents = new SysArea();
            areaParents.setId(sysArea.getId());
            areaParents.setParentIds(sysArea.getParentIds());
            areaParents.setNewId(s);
            sysAreaSerevice.updateParentIds(areaParents);
           sysArea.setParentIds(s);
           sysArea.setParentId(Long.valueOf(newId));
        }
        return sysAreaSerevice.updateByPrimaryKeySelective(sysArea);
    }

    @RequestMapping("/showDetail/{id}")
    public SysArea showDetail(@PathVariable("id")Long id){
        return sysAreaSerevice.selectByPrimaryKey(id);
    }
    @RequestMapping("toSelect")
    public ModelAndView toSelect(){
        return new ModelAndView("area/select");
    }
    @RequestMapping("detailView")
    public ModelAndView toDetail(){
        return new ModelAndView("area/detail");
    }
    @RequestMapping("/toIcon")
    public ModelAndView toIcon(){
        return new ModelAndView("modules/font-awesome");
    }
    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(){
        return new ModelAndView("area/update");
    }
    @RequestMapping("/insertOne")
    public ModelAndView insertOne(){
        return new ModelAndView("area/insert");
    }

    @RequestMapping("/showInsert/{id}")
    public SysArea showInsert(@PathVariable("id")Long id){
        SysArea sysArea = sysAreaSerevice.selectByPrimaryKey(id);
        sysArea.setParentId(sysArea.getId());
        sysArea.setParentName(sysArea.getName());
        sysArea.setId(null);
        sysArea.setName(null);
        sysArea.setCode(null);
        Integer type = Integer.valueOf(sysArea.getType())+1;
        sysArea.setType(type.toString());
        return sysArea;
    }
    @RequestMapping("/doInsert")
    public int showInsert(@RequestBody SysArea sysArea){
        System.out.println(sysArea);
        sysArea.setParentIds(sysArea.getParentIds()+sysArea.getParentId()+",");
        sysArea.setCreateDate(new Date());
        sysArea.setUpdateDate(new Date());
        return sysAreaSerevice.insertSelective(sysArea);
    }

    @RequestMapping("upload")
    public Result upload(MultipartFile file) throws IOException {
            sysAreaSerevice.upload(file.getInputStream());
        return new Result(true,"上传成功",null);
    }

    @RequestMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        /**
         * 1.设置响应头，告诉浏览器以附件的方式下载到客户端(content-disposition: attachment;filename='xxx')
         * 2.读取数据，写入到excel
         * 3.获取response的输出流，将excel放入输出
         */
        String fileName = new String("区域列表.xlsx".getBytes(),"iso-8859-1");//解决文件名乱码
        response.setHeader("content-disposition","attachment;filename="+fileName);
        sysAreaSerevice.download(response.getOutputStream());
    }
}
